﻿using Furion.DatabaseAccessor;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;

namespace Furion.Core
{

    /// <summary>
    /// 用户表
    /// </summary>
    [Comment("用户表")]
    public class User: IEntity, IEntityTypeBuilder<User>, IEntitySeedData<User>
    {
        public User()
        {
            CreatedTime = DateTime.Now;           
            Enabled = true;
            UpdatedTime = DateTime.Now;
            
        }

        //[Key]
        //[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        //public int Id { get; set; }

        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.None), Comment("主键")]
        public long Id { get; set; }

        /// <summary>
        /// 账号
        /// </summary>
        [Required, MaxLength(32), Comment("账号")]
        public string UserName { get; set; }

        /// <summary>
        /// 密码（采用 MD5 加密）
        /// </summary>
        [Required, MaxLength(32), Comment("密码")]
        public string UserPass { get; set; }

        /// <summary>
        /// 昵称
        /// </summary>
        [MaxLength(32), Comment("昵称")]
        public string NickName { get; set; }

        /// <summary>
        /// 头像（OSS地址）
        /// </summary>
        [MaxLength(200), Comment("头像")]
        public string ImageUrl { get; set; }

        /// <summary>
        /// 简介
        /// </summary>
        [MaxLength(200), Comment("简介")]
        public string Remark { get; set; }
         
        /// <summary>
        /// 手机
        /// </summary>
        [MaxLength(50), Comment("手机")]
        public string Mobile { get; set; }



        /// <summary>
        /// 部门Id
        /// </summary>
        [Comment("部门Id")]
        public int OrgId { get; set; } = 0;


        /// <summary>
        /// 最近登录时间
        /// </summary>
        [Comment("最近登录时间")]
        public DateTime? LoginTime { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        [Comment("是否启用")]
        public bool Enabled { get; set; } = true;


        /// <summary>
        /// 微信OpenId
        /// </summary>
        [Comment("微信OpenId")]
        public string OpenId { get; set; }





        /// <summary>
        /// 
        /// </summary>
        [Comment("创建时间")]
        public DateTime CreatedTime { get; set; }


        /// <summary>
        /// 
        /// </summary>
        [Comment("更新时间")]
        public DateTime UpdatedTime { get; set; }

        /// <summary>
        /// 创建者Id
        /// </summary>
        [Comment("创建者Id")]
        public long? CreatedUserId { get; set; }

        /// <summary>
        /// 创建者名称
        /// </summary>
        [Comment("创建者名称")]
        [MaxLength(20)]
        public  string CreatedUserName { get; set; }

        /// <summary>
        /// 修改者Id
        /// </summary>
        [Comment("修改者Id")]
        public long? UpdatedUserId { get; set; }

        /// <summary>
        /// 修改者名称
        /// </summary>
        [Comment("修改者名称")]
        [MaxLength(20)]
        public  string UpdatedUserName { get; set; }

        /// <summary>
        /// 软删除
        /// </summary>
        [JsonIgnore]
        [Comment("软删除标记")]
        public  bool IsDeleted { get; set; } = false;




        /// <summary>
        /// 多对多
        /// </summary>
        public ICollection<Role> Roles { get; set; }

        /// <summary>
        /// 多对多中间表
        /// </summary>
        public List<UserRole> UserRoles { get; set; }



        /// <summary>
        /// 配置多对多关系
        /// </summary>
        /// <param name="entityBuilder"></param>
        /// <param name="dbContext"></param>
        /// <param name="dbContextLocator"></param>
        public void Configure(EntityTypeBuilder<User> entityBuilder, DbContext dbContext, Type dbContextLocator)
        {
            entityBuilder.HasMany(p => p.Roles)
                 .WithMany(p => p.Users)
                 .UsingEntity<UserRole>(
                   u => u.HasOne(c => c.Role).WithMany(c => c.UserRoles).HasForeignKey(c => c.RoleId)
                 , u => u.HasOne(c => c.User).WithMany(c => c.UserRoles).HasForeignKey(c => c.UserId)
                 , u =>
                 {
                     u.HasKey(c => new { c.UserId, c.RoleId });
                     u.HasData(
                         new { UserId = long.Parse("1"), RoleId = 1 }
                     );
                 });
        }

        /// <summary>
        /// 种子数据
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="dbContextLocator"></param>
        /// <returns></returns>
        public IEnumerable<User> HasData(DbContext dbContext, Type dbContextLocator)
        {
            return new[]
            {
                new User
                {
                    Id=long.Parse("1"),UserName="admin",UserPass="e10adc3949ba59abbe56e057f20f883e", CreatedTime=DateTime.Now,Enabled=true, ImageUrl="", IsDeleted=false, LoginTime=DateTime.Now, NickName="管理员",
                    OpenId="", OrgId=0, Remark="", UpdatedTime=DateTime.Now
                }
            };
        }


    }
}
